import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Collapse, Spin} from 'antd';
import {FILES_API} from '../constants';
import {conventEnumValueToString, formatAmount} from '../utils';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {currency} from '../Enum';
import routes from './routes';
import {getDetail} from './api';
import styles from './style.css';
import DetailTable from './DetailTable';
import {injectIntl, formatMessage} from './intl';
export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {
            goodwillSettlementDetails: []
        }
    };
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        this.setState({loading: true});
        // eslint-disable-next-line react/prop-types
        getDetail(this.props.id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }

    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }

    render() {
        const {data = {}, loading} = this.state;
        const operations = [];
        const packingPanel = data.goodwillSettlementDetails.map(item => {
            const header = (
                <Row>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'detailPage.info.goodWillCode',
                            defaultMessage: 'GoodWill编号'
                        })}: ${item.goodWillCode}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'detailPage.info.laborFee',
                            defaultMessage: '工时费'
                        })}: ${formatAmount(item.laborFee)}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'detailPage.info.materialFee',
                            defaultMessage: '材料费'
                        })}: ${formatAmount(item.materialFee)}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'detailPage.info.totalFee',
                            defaultMessage: '费用合计'
                        })}: ${formatAmount(item.totalFee)}`}
                    </Col>
                </Row>
            );
            const dataSource = item.goodWillDetails || [];
            return (
                <Collapse.Panel key={item.id} header={header}>
                    <DetailTable data={dataSource} />
                </Collapse.Panel>
            );
        });
        return (
            <div className={styles.detail}>
                <Spin spinning={loading}>
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'detailPage.card.tradeMethodmessage',
                                defaultMessage: '基本信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                        </Row>}>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.code',
                                defaultMessage: '编号',
                            })}>{data.code}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.brand',
                                defaultMessage: '品牌'
                            })}>{`(${data.brandCode})${data.brandName}`}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.dealer',
                                defaultMessage: '总代',
                            })}>{`(${data.dealerCode})${data.dealerName}`}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.currency',
                                defaultMessage: '币种'
                            })}>{conventEnumValueToString(currency, data.currency)}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.country',
                                defaultMessage: '国家'
                            })}>{data.country}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.laborFee',
                                defaultMessage: '工时费合计',
                            })}>{formatAmount(data.laborFee)}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.materialFee',
                                defaultMessage: '材料费'
                            })}>{formatAmount(data.materialFee)}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.totalFee',
                                defaultMessage: '费用合计'
                            })}>{formatAmount(data.totalFee)}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.comment',
                                defaultMessage: '业务内容'
                            })}>{data.comment}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</DescriptionList.Description>
                        </DescriptionList>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'detailPage.details.title',
                            defaultMessage: 'GoodWill结算清单'
                        })}
                        bordered={false}>
                        <Collapse>
                            {packingPanel}
                        </Collapse>
                    </Card>
                    {data.attachments && data.attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'detailPage.card.attachment',
                            defaultMessage: '附件'
                        })}>
                            {data.attachments.map(item => (<DescriptionList.Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></DescriptionList.Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'detailPage.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'detailPage.card.comment',
                                defaultMessage: '意见'
                            })
                        }</Col>
                        </Row>}>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.uplpadComment',
                                defaultMessage: '上传意见'
                            })}>{data.uplpadComment}</DescriptionList.Description>
                            <DescriptionList.Description term={formatMessage({
                                id: 'detailPage.data.approveComment',
                                defaultMessage: '审核意见'
                            })}>{data.approveComment}</DescriptionList.Description>
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
